/*
	File: D:/works_2/panzer_2010/data/scripts/saucer_body_light_script.nut
	Author: The Team
*/

	Include("data/scripts/globals.nut")

/*!
	@short	ring_spot
	@author	The Team
*/
class	ring_spot
{
/*<
	<Script =
		<Name = "Light Script">
		<Author = "Thomas Simonnet">
		<Description = "The light change by proximity player">
		<Compatibility = <Item>>
	>
	<Parameter =
		<distance = <Name = "Distance"> <Type = "Float"> <Default = 1.0>>		
	>
>*/

	//--------------------------------------------------------------------------
	distance				=	1.0

	//--------------------------------------------------------------------------
	target_name			=	""
	target_item			=	0		
	distance_sq 			=	0
	light_handler			=	0

	current_vibration 		=	0

	cone_angle 			=	0

	prev_pos				=	0
	/*!
		@short	OnUpdate
		Called during the scene update, each frame.
	*/
	function	OnUpdate(item)
	{
		if(target_item)
		{
			local desired_dir_vect = ItemGetWorldPosition(target_item) - ItemGetWorldPosition(item)

			local current_dist_to_target_sq = desired_dir_vect.Len2()
			if(current_dist_to_target_sq > distance_sq)
				LightSetConeAngle(light_handler, RangeAdjust(Clamp(current_dist_to_target_sq, distance_sq, distance_sq*3.0), distance_sq, distance_sq*3.0, cone_angle, cone_angle * 0.75))
			else
			{			
				current_vibration += 0.1
				LightSetDiffuseIntensity(light_handler, fabs(cos(current_vibration))*5.0+2.0)
			}
		}
		
		local	_vel,_color
		
		_vel = ItemGetWorldPosition(item).Dist(prev_pos)
		prev_pos = ItemGetWorldPosition(item)
		
		_vel = Clamp(RangeAdjust(_vel, 0.0005, 0.015, 0.0, 1.0), 0.0, 1.0)

		_color = colors.orange.Lerp(_vel, colors.blue)
		LightSetDiffuseColor(light_handler, _color)
		
	}

	/*!
		@short	OnSetup
		Called when the item is about to be setup.
	*/
	function	OnSetupDone(item)
	{
		target_name = "tank"
		if(target_name && target_name != "")
		{
			target_item = SceneFindItem(g_scene, target_name)
			if	(!ObjectIsValid(target_item))
				target_item = 0
		}
		distance_sq = distance*distance

		light_handler = ItemCastToLight(item)
		cone_angle = LightGetConeAngle(light_handler)
		prev_pos = ItemGetWorldPosition(item)
	}
}
